'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

from carcomp.apps.gamma import hjoin, bloomhj
from carcomp.apps.gamma.util import tuple_sort
from carcomp.framework import wrapper
from carcomp.directory import localhost
import unittest, filecmp

class Test(unittest.TestCase):
    
    #a_tpl = ('A', localhost.local_directory.add_root('ft/gamma/data/i_Bloom_abs7-sendA_o_HashJoin_abs3-A.txt'))
    #b_tpl = ('B', localhost.local_directory.add_root('ft/gamma/data/i_BFilter_abs11-BPrime_o_HashJoin_abs3-B.txt'))
    #out_tpl = ('AB', localhost.local_directory.add_root('ft/gamma/data/i_HashJoin_abs3-AB_o_Merge_abs8-in3.txt'))
    a_tpl = ('A', localhost.local_directory.add_root('ft/gamma/TEST_INPUT_A.txt'))
    b_tpl = ('B', localhost.local_directory.add_root('ft/gamma/TEST_INPUT_B.txt'))
    out_tpl = ('AB', localhost.local_directory.add_root('ft/gamma/TEST_OUTPUT.txt'))
    
    
    def test_simple_hjoin(self):
        hj_abs = hjoin.HashJoinAbs('HashJoin_abs3')
        hjw = wrapper.Wrapper()
        hjw.add_abstraction(hj_abs, [Test.a_tpl, Test.b_tpl], [Test.out_tpl])
        hjw.run()
        ts = tuple_sort.TupleFileSort('{}{}'.format(Test.out_tpl[1], '_TEST.txt'))
        temp_name = ts.create_temp_file()
        ret = filecmp.cmp(Test.out_tpl[1], temp_name, False)
        ts.cleanup()
        self.assertEqual(ret, True)
                          
    def test_bloom_hjoin(self):
        hj_abs = bloomhj.BloomHashJoinAbs('HashJoin_abs3')
        hjw = wrapper.Wrapper()
        hjw.add_abstraction(hj_abs, [Test.a_tpl, Test.b_tpl], [Test.out_tpl])
        hjw.run()
        ts = tuple_sort.TupleFileSort('{}{}'.format(Test.out_tpl[1], '_TEST.txt'))
        temp_name = ts.create_temp_file()
        ret = filecmp.cmp(Test.out_tpl[1], temp_name, False)
        ts.cleanup()
        self.assertEqual(ret, True)
        
    def test_parallel_hjoin(self):
        hj_abs = bloomhj.ParBloomHashJoinAbs('HashJoinAbs_alg0')
        hjw = wrapper.Wrapper()
        hjw.add_abstraction(hj_abs, [Test.a_tpl, Test.b_tpl], [Test.out_tpl])
        hjw.run()
        ts = tuple_sort.TupleFileSort('{}{}'.format(Test.out_tpl[1], '_TEST.txt'))
        temp_name = ts.create_temp_file()
        ret = filecmp.cmp(Test.out_tpl[1], temp_name, False)
        ts.cleanup()
        self.assertEqual(ret, True)
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_instrument']
    unittest.main()       